package joctave.toolbox.joctave.image;

import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import joctave.core.tokens.*;
import joctave.core.tokens.numbertokens.DoubleNumberToken;
import joctave.core.functions.ExternalFunction;
import joctave.core.interpreter.GlobalValues;
import joctave.toolbox.joctave.matrix._private.Jama.EigenvalueDecomposition;

/**An external function for computing eigenvalues and eigenvectors of an array  */
public class imread extends ExternalFunction
{
	  // public static BufferedImage convertToGray(BufferedImage im)
	 /* public static BufferedImage toScaledGray(BufferedImage im, float scale)
	  // scale and convert to grayscale 
	  {
	    int imWidth = im.getWidth();
	    int imHeight = im.getHeight();

	    int nWidth = (int)Math.round(imWidth * scale);
	    int nHeight = (int)Math.round(imHeight * scale);

	    // convert to grayscale while resizing
	    BufferedImage grayIm = new BufferedImage(nWidth, nHeight, 
	                                            BufferedImage.TYPE_BYTE_GRAY);  
	    Graphics2D g2 = grayIm.createGraphics();
	    g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, 
	                        RenderingHints.VALUE_INTERPOLATION_BILINEAR);
	    g2.drawImage(im, 0, 0, nWidth, nHeight,  0, 0, imWidth, imHeight, null);  
	    g2.dispose();  

	    return grayIm;
	    
	  } */ // end of toScaledGray()	
	/**return a  matrix 
	* @param operands[0] = matrix 
	* @return matrix                                 */
	public OperandToken evaluate(Token[] operands, GlobalValues globals)
	{

		// one operand (e.g. eig(A) )
		if (getNArgIn(operands) !=1)
			throwMathLibException("imread: number of arguments !=1");

	    if (!(operands[0] instanceof CharToken)) 
        	throwMathLibException("imread: argument must be a string");

	    String filename = ((CharToken)operands[0]).toString();

	    BufferedImage bitmap=null;
	    try {
	        bitmap = ImageIO.read(new File(filename));
	        
	    } catch (IOException e) {
	        throwMathLibException("imread: invalid file"+ System.getProperty("user.dir"));
	    }
	   // bitmap=toScaledGray(bitmap,1);
	    int numcols=bitmap.getWidth();
	    int numrows=bitmap.getHeight();
	    System.out.println("dmmm"+bitmap.getType());
	    if (bitmap.getType()==BufferedImage.TYPE_BYTE_INDEXED) {//TYPE_BYTE_GRAY) {
		    double []imdata=new double[numcols*numrows];
		    bitmap.getData().getPixels(0, 0, numcols, numrows, imdata);
	
			double [][] X = new double[bitmap.getWidth()][numrows];
		    for (int i = 0; i < numcols; i++)
		    	for (int j = 0; j < numrows; j++)
		    		X[i][j]=imdata[i+j*numcols];

	        return new DoubleNumberToken(X);
	    } else {
		   // double []imdata=new double[numcols*numrows*4];
		    //bitmap.getData().getPixels(0, 0, numcols, numrows, imdata);
	        int[] dim = new int[3];
	        dim[0]=bitmap.getWidth();
	        dim[1]=numrows;
	        dim[2]=3;
	        DoubleNumberToken num = new DoubleNumberToken(dim, null, null);
	        //num.set
			//double [][][] X = new double[bitmap.getWidth()][numrows][3];
		    for (int i = 0; i < numcols; i++)
		    	for (int j = 0; j < numrows; j++) {
		    		int pixel=bitmap.getRGB(i,j);//(int)imdata[i+j*numcols];
		    		int r=((pixel >> 16) & 0xFF);
		    		int g=((pixel >> 8) & 0xFF);
		    		int b=((pixel) & 0xFF);
		    		num.setValue(new int[] {i, j, 0},r,0);
		    		num.setValue(new int[] {i, j, 1},g,0);
		    		num.setValue(new int[] {i, j, 2},b,0);
		    		/*X[i][j][0]=r;
		    		X[i][j][1]=g;
		    		X[i][j][2]=b;*/
		    	}

	        return num;// new DoubleNumberToken(X);
	    }
		

	} // end eval
}

/*
@GROUP
image
@SYNTAX
imread(filename)
@DOC
loads an image
@NOTES
@EXAMPLES
<programlisting>
imread('foo.png')
</programlisting>
@SEE
*/

